package com.example.nfcaccesssystem;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class DoorActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    setContentView(R.layout.activity_door);
		
	    ListView lw = (ListView)findViewById(R.id.doorListView);
		
		lw.setOnItemClickListener(new OnItemClickListener() {
	    	
	    	@Override
	    	public void onItemClick(AdapterView<?> adapter, View view, int position, long arg){
	    		Door d = (Door) adapter.getItemAtPosition(position);
	    		
	    		Intent i = new Intent(DoorActivity.this, EditDoorActivity.class);
	    		i.putExtra("door", d);
	    		startActivity(i);
	    	}
		});
	}
	
	@Override
	public void onResume() {
		super.onPause();
		
		Controller.getInstance().getDoors(this);
	}

	public void newDoor(View v) {
		Intent i = new Intent(this, NewDoorActivity.class);
		startActivity(i);
	}
	
	public void setDoors(Door[] doors) {
		
		ListView lw = (ListView)findViewById(R.id.doorListView);
		
	        if (lw.getAdapter() == null) {
		        final ArrayAdapter<Door> adapter = new ArrayAdapter<Door>(this, android.R.layout.simple_list_item_1, Controller.getInstance().doorList);
			        
			    lw.setAdapter(adapter);
	        }
	        
	        ArrayAdapter<Door> adap = (ArrayAdapter<Door>)lw.getAdapter();
	        
	        for (Door d : doors) {
	        	adap.add(d);
	        }
	        
	        adap.notifyDataSetChanged();
	}
	
	
}
